var express = require('express');
const userDataModel = require('../model/userDataModel');
const addDataModel = require('../model/addDataModel');
const orderModel = require('../model/orderModel');
const ifToken = require('../utils/token')
const VipUerModel = require('../model/vipUserModel')
var router = express.Router();

/* GET users listing. */
router.post('/user', ifToken, async (req, res, next) => {
  const { mail } = req.body
  const userData = await userDataModel.findOne({ _id: mail })
  if (!userData) return res.json({ code: '300', msg: '未登录' })
  res.json({ code: '200', data: userData })
});
//用户发布订单数量
router.post('/userDataSepmt', ifToken, async (req, res, next) => {
  const { mail } = req.body
  const userData = await addDataModel.find({ userid: mail })
  res.json({ code: '200', data: userData.length })
});
//用户接取订单数量
router.post('/userjiequnum', ifToken, async (req, res, next) => {
  const { mail } = req.body
  const userData = await orderModel.find({ mailId: mail })
  res.json({ code: '200', data: userData.length })
});
//用户发布订单数据
router.post('/userSepmt', ifToken, async (req, res, next) => {
  const { mail } = req.body
  const userData = await addDataModel.find({ userid: mail })
  res.json({ code: '200', data: userData })
});
//用户接取订单数据
router.post('/userJQSepmt', ifToken, async (req, res, next) => {
  const { mail } = req.body
  const newdata = await orderModel.find({ mailId: mail })
  const promises = newdata.map(async item => {
    const data = await addDataModel.findOne({ _id: item.orderId });
    return data;
  });

  const arr = await Promise.all(promises);
  res.json({ code: '200', data: arr })
});
//接取订单
router.post('/userOrder', ifToken, async (req, res, next) => {
  const { id, mail } = req.body
  const userData = await userDataModel.findOne({ _id: mail })
  const vipuser = await VipUerModel.findOne({ mail: userData.mail })
  if(!vipuser) return res.json({ code: '304', msg: '请开通会员在进行接单' })
  const data = await orderModel.findOne({ orderId: id, mailId: mail })
  if (data) return res.json({ code: '304', msg: '已接取此单，请添加发帖人联系方式' })
  await orderModel.create({ orderId: id, mailId: mail })
  const dataNum = await orderModel.find({ orderId: id })
  res.json({ code: '200', data: dataNum.length })
});
//此订单接单用户数量
router.post('/userOrderNum', async (req, res, next) => {
  const { id } = req.body
  const data = await orderModel.find({ orderId: id })
  res.json({ code: '200', data: data.length })
});
//检查是否接取了此单
router.post('/ifUserOrder', async (req, res, next) => {
  const { id, mail } = req.body
  const data = await orderModel.findOne({ orderId: id, mailId: mail })
  if (data) return res.json({ code: '200', msg: '已接取此单，请添加发帖人联系方式' })
  res.json({ code: '300' })
});

//发布者的联系方式
router.post('/ifUserPhone', async (req, res, next) => {
  const { id } = req.body
  const data = await addDataModel.findOne({ _id: id })
  const userphone = await userDataModel.findOne({ _id: data.userid })
  res.json({ code: '200', data: userphone.phone })
});

//验证是否是会员用户
router.post('/isVipUser',ifToken, async (req, res, next) => {
  const { id } = req.body
  const data = await userDataModel.findOne({ _id: id })
  const vipuser = await VipUerModel.findOne({ mail: data.mail })
  if (vipuser) return res.json({ code: '200', data: vipuser.expiresAt })
  res.json({ code: '300', msg: '不是会员账号'  })
});

module.exports = router;
